<?php

include_once "quickcrud_connect.php";
include_once "quickcrud_common.php";
include "CF_mysql_com.php";
include_once "CF_all_com.php";

if (!isset($_REQUEST['crudset_id']) OR !isset($_REQUEST['project_id']) ){
	$result =  'Please select or create a project and set to use first';
	$result = urlencode($result);
	if($_REQUEST['output'] == 'screen'){
		print $result;
		exit;
	}else{
	
		$header_text = "Location: leftmenu.php?project_id=$project_id&crudset_id=$crudset_id&result=$result";
		header($header_text); /* Redirect browser */
		exit;
	}
}

	$project_id = $_REQUEST['project_id'];
	$crudset_id = $_REQUEST['crudset_id'];


	$qry_project_info = "SELECT * FROM projects WHERE project_id = $project_id";

	$project_info = get_array_query($qry_project_info);

	$project_db = $project_info[1]['project_db'];
	$project_path = $project_info[1]['project_path'];
	$project_server = $project_info[1]['project_server'];
	$project_login = $project_info[1]['project_login'];
	$project_pass = $project_info[1]['project_pass'];
	$project_name = $project_info[1]['project_name'];
    $class_subpath = $project_info[1]['class_subpath'];
	$template_dir = $project_info[1]['template_dir'];	

    if(!empty($class_subpath)){
        $class_subpath = $class_subpath . '/';
        }


	$qry_set_info = "SELECT * FROM crudsets
            JOIN tablesources ON tablesources.tablesource_id = crudsets.main_table
    WHERE crudset_id = $crudset_id";

	$set_info = get_array_query($qry_set_info);

// fields from database
	$cs_name = $set_info[1]['cs_name'];
	$paging = $set_info[1]['paging'];
	$main_table_name = $set_info[1]['table_name'];
	$cs_type = $set_info[1]['cs_type'];
    $proc_base_name = $set_info[1]['proc_base_name'];
    $process_class = $set_info[1]['process_class'];
    $html_table_name = $set_info[1]['html_table_name'];
    $proc_class_instance = $set_info[1]['proc_class_instance'];
	$subdirectory = $set_info[1]['subdirectory'];
	$needs_login = $set_info[1]['needs_login'];


// calculated fields
    $process_page = $cs_name . '_proc.php';
    $list_page = $cs_name . '_list.php';

// Calcuate back directory $backdirectory from levels in subdirectory
	$subdirlevels = substr_count($subdirectory,'/');
	while ($subdirlevels > 0){
		$backdirectory .= "../";
		$subdirlevels --;
	}		
// comment out login logic or not
	if($needs_login == 1){
		$no_login = '';
	}
	else{
		$no_login = '//';
	}

// if subdirectory path not exists, create
if(!empty($subdirectory)){
	$subdirname = rtrim($subdirectory, "/");
		if(!file_exists('projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirname))
		{
			mkdir('projects/' . $project_path . "/" . WEBAPP_SUBPATH . '/' . $subdirname);
		}
	}
		
	
	
	$str_row_limit = ". \" \$" . $list_table_instance . "->post_rowlimit\"";
	$str_qry_sort = " . \$" . $list_table_instance . "->make_query_sort()";
	$str_qry_filter = ". \$" . $list_table_instance . "->make_query_filter()";

	$str_query = "SELECT * FROM $table_name WHERE 1 ";


	$str_list_file = file_get_contents($template_dir . 'procTPL.php');

    $str_list_file = str_replace("%class_subpath%",$class_subpath,$str_list_file);
    $str_list_file = str_replace("%subdirectory%",$subdirectory,$str_list_file);
    $str_list_file = str_replace("%backdirectory%",$backdirectory,$str_list_file);
    $str_list_file = str_replace("%no_login%",$no_login,$str_list_file);
	
	$str_list_file = str_replace("%process_page%",$process_page,$str_list_file);
	$str_list_file = str_replace("%list_page%",$list_page,$str_list_file);
	$str_list_file = str_replace("%process_class%",$process_class,$str_list_file);
	$str_list_file = str_replace("%proc_base_name%",$proc_base_name,$str_list_file);
	$str_list_file = str_replace("%cs_name%",$cs_name,$str_list_file);
	$str_list_file = str_replace("%header_type%",$header_type,$str_list_file);

    $str_list_file = str_replace("%main_table_name%",$main_table_name,$str_list_file);



	$str_list_file = str_replace("%connectinclude%",$project_name . "_connect.php",$str_list_file);
	$str_list_file = str_replace("%fieldinclude%",$cs_name . "_fields.php",$str_list_file);
	$str_list_file = str_replace("%cs_name%",$cs_name,$str_list_file);
	$str_list_file = str_replace("%str_key%",$str_key,$str_list_file);

    $str_list_file = str_replace("%proc_class_instance%",$proc_class_instance,$str_list_file);



	



if($_REQUEST['output'] == 'screen')	{
	echo "<html>\n<head>\n</head>\n<body>";
    echo "<TEXTAREA NAME=\"comments\" COLS='80' ROWS='20'>$str_list_file</TEXTAREA>\n";
    echo "</body></html>";


    }else{
        file_put_contents('projects/' . $project_path . "/" .  WEBAPP_SUBPATH . '/' .$subdirectory . $cs_name . "_proc.php",$str_list_file);
        $result = "Wrote Processing Page To $output_file";
        $result = urlencode($result);

        $header_text = "Location: leftmenu.php?project_id=$project_id&crudset_id=$crudset_id&result=$result";
        header($header_text); /* Redirect browser */
        exit;
	}


?>
